<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="toSave(0)">
        添加
      </el-button>
      <el-button class="filter-item" type="warning" icon="el-icon-delete" @click="handleFrozen">
        批量上下架
      </el-button>
      <el-button class="filter-item" type="danger" icon="el-icon-delete" @click="handlePlDelete">
        批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="序号" prop="sequence" align="center" />
      <el-table-column label="标题" prop="name" align="center"/>
      <el-table-column label="图片" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 100px; height: 100px" v-if="row.image" :src="row.image"
            :preview-src-list="[row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center">
        <template slot-scope="{row}">
        <el-tag v-if="row.isShow" type="success">上架</el-tag>
        <el-tag v-else type="danger">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="toSave(row.id)" >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { indexRollPageByQo, indexRollRemoveByIds, indexRollUpByIds } from '@/api/zcyx'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'MicroIndexRoll',
  directives: { waves },
  components: { Pagination },
  props: {
    type: {
      default: 0,
      type: Number
    }
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        gongyingId: '',
        pageNo: 1,
        pageSize: 20,
        name$lk: '',
        paramType: '',
        code: 'weiIndexRoll'
      },
      multipleSelection: [],
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const t = route.query.t;
        if (t) {
          this.handleFilter()
        }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNo = 1;
      this.getList();
    },
    getList() {
      this.listLoading = true

      this.listQuery.gongyingId = this.userInfo.id
      indexRollPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    toSave(id) {
      this.$router.push({ path: `/microShop/microIndexRollSave`, query: { id: id } })
    },
    // 批量操作
    handleSelectionChange(val) {
      this.multipleSelection = val.map((ele) => {
        return ele.id
      })
    },
    handleFrozen(){
      if (!this.multipleSelection || this.multipleSelection.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      const params = {
        ids: this.multipleSelection.join(',')
      }
      this.$confirm('是否继续操作？').then(() => {
        indexRollUpByIds(params).then(() => {
          this.$notify({
            title: '成功',
            message: '批量操作成功！',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    },
    handlePlDelete() {
      if (!this.multipleSelection || this.multipleSelection.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      const params = {
        ids: this.multipleSelection.join(',')
      }
      this.$confirm('是否继续删除？').then(() => {
        indexRollRemoveByIds(params).then(() => {
          this.$notify({
            title: '成功',
            message: '批量删除成功！',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    }
  }
}
</script>
